package com.itic.web.knowledge.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.plugins.mybatis.Page;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.actcms.knowledge.persistence.model.WebKnowledge;
import com.itic.ht.actcms.knowledge.persistence.model.WebKnowledgeExample;
import com.itic.ht.actcms.knowledge.service.WebKnowledgeService;
import com.itic.ht.actcms.knowledgedetail.persistence.model.WebKnowledgeDetail;
import com.itic.ht.actcms.knowledgedetail.persistence.model.WebKnowledgeDetailExample;
import com.itic.ht.actcms.knowledgedetail.service.WebKnowledgeDetailService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.Datav;
import com.itic.web.ro.PageInfo;
import com.itic.web.ro.ResResult;
import com.itic.web.ro.RoResult;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class AppKnowledgeService {
	

	@Autowired
	private WebMemberService webMemberService;
    @Autowired
    private WebKnowledgeService knowledgeService;
    @Autowired
    private WebKnowledgeDetailService knowledgeDetailService;
    @Autowired
    private ModelFilesService modelFilesService;

	
	public ResResult getIndex(TokenData data,Page page){
		
		List<WebKnowledge> knowledgeList = null;
		try {
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//page对象不能为空
	        if (page.getPageNow()>0 && page.getPageSize()>0) {
	        }else{
	        	return ResResult.error(3, "page数据不能为空");
	        }
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			page.setRecordNow((page.getPageNow()-1)*page.getPageSize());
			
			//查询knowledgeList
			WebKnowledgeExample knowledgeExample = new WebKnowledgeExample();
			knowledgeExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
			knowledgeExample.setOrderByClause("REORDER ASC");
			knowledgeExample.setPage(page);
	        // 获取信息总条数
	        int total = knowledgeService.count(knowledgeExample);
	        System.out.println("现在查询的总条数是:"+total+"个");
	        
			knowledgeList = knowledgeService.list(knowledgeExample);
			if(knowledgeList!=null && knowledgeList.size()>0){
			     for (WebKnowledge webKnowledge : knowledgeList) {
                  	String knowledgefileId = webKnowledge.getFileId();
                  	if(knowledgefileId!=null && !"".equals(knowledgefileId)){
                  		webKnowledge.setFileId(modelFilesService.getFile(knowledgefileId).getFilePath());
                  	}
			     }
			}else{
				knowledgeList = new ArrayList<WebKnowledge>();
			}
			
			PageInfo instance = PageInfo.getInstance(page.getPageNow(), page.getPageSize(), total);
			
			return ResResult.build(200, "查询成功",knowledgeList,instance);
		} catch (Exception e) {
			//返回错误代码
			return ResResult.error(4, "查询失败");
		}
		
	}
	
	public RoResult getDetailList(TokenData data,String pid){
		
		
		List<WebKnowledgeDetail> knowledgeDetailList = null;
		try {
			if(StringHelper.isEmpty(data.getToken())){
				return RoResult.error(1, "token不能为空");
			}
			//先判断
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return RoResult.error(2, "token失效");
			}
			//page对象不能为空
			if (pid == null   && "".equals(pid)) {
				return RoResult.error(3, "pid不能为空");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			//查看主题数据
			WebKnowledge webKnowledge = knowledgeService.get(pid);
			//查询knowledgeDetailList
			WebKnowledgeDetailExample knowledgeDetailExample = new WebKnowledgeDetailExample();
			knowledgeDetailExample.createCriteria().andPidEqualTo(pid).andIsEffectiveEqualTo(DBConst.TRUE);
			knowledgeDetailExample.setOrderByClause("REORDER ASC");
			
			knowledgeDetailList = knowledgeDetailService.list(knowledgeDetailExample);
			System.out.println("knowledgeDetailList查询到数据个数据为:"+knowledgeDetailList.size());
			if(knowledgeDetailList!=null && knowledgeDetailList.size()>0){
				
				for (WebKnowledgeDetail webKnowledgeDetail : knowledgeDetailList) {
					String knowledgeDetailfileId = webKnowledgeDetail.getFileId();
					if(knowledgeDetailfileId!=null && !"".equals(knowledgeDetailfileId)){
						webKnowledgeDetail.setFileId(modelFilesService.getFile(knowledgeDetailfileId).getFilePath());
						webKnowledgeDetail.setNote(modelFilesService.getFile(knowledgeDetailfileId).getVedioPath());
					}
				}
				
			}else{
				knowledgeDetailList = new ArrayList<WebKnowledgeDetail>();
			}
			Datav datas = new Datav();
			datas.setData(webKnowledge);
			datas.setDatas(knowledgeDetailList);
			
			return RoResult.build(200, "查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			return RoResult.error(4, "查询失败");
		}
		
	}
	
	
}
